`
https://leetcode.cn/problems/closest-nodes-queries-in-a-binary-search-tree/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number[]} queries
 * @return {number[][]}
 */
var closestNodes = function (root, queries) {
  // 题目不保证 BST 是平衡的，所以实际上有可能退化成链表的
  // 需要先中序遍历一遍，得到有序数组，再二分查找
  const incr = []
  const traverse = (root) => {
    if (!root) return
    traverse(root.left)
    incr.push(root.val)
    traverse(root.right)
  }
  traverse(root)
  // 此时 incr 就是有序的，用二分查一遍就行
  return queries.map((q) => {
    // <= q
    const minVal = _.sortedIndex(incr, q + 1) - 1
    // >= q
    const maxVal = _.sortedIndex(incr, q)
    return [
      minVal !== -1 ? incr[minVal] : -1,
      maxVal !== incr.length ? incr[maxVal] : -1
    ]
  })
};